const { Controller } = require("uni-cloud-router")
const db = uniCloud.database()
const dbCmd = db.command
const formdb = db.collection('form')
const formitemdb = db.collection('form-item')
const formsubmitdb = db.collection('form-submit')
const uniID = require('uni-id')

module.exports = class FormController extends Controller {
  sayHello() {
    return this.service.hello.sayHello();
  },
	async submit(){
		const {ctx, service} = this;
		// 参数校验-格式校验
		// ctx.validator({
		// 	title: {type: "string"}
		// })
		let result = {
			code: 0,
			msg: ''
			data: {}
		}
		const auth = ctx.auth.uid
		var param = ctx.data
		try {
			let info = await formdb.doc(param.form_id).get()
			info = info.result.data
			if(!info) {
				result.msg = '相关表单不存在或已失效'
				result.code = 500
				return result
			}
			if (info.islogin == 1 && !auth) {
				result.code = 430
				result.msg = '请登录后再操作'
				return result
			}
			
			return service.form.submit(param, auth)
		}catch(err){
			console.log(err)
			result.code = 400
			result.msg = err.message
			return result
		}
	},
	submitDetail(){
		const {ctx, service} = this
		let result = {
			code: 0,
			msg: ''
			data: {}
		}
		const auth = ctx.auth.uid
		var param = ctx.data
		result.data = db.collection('form-submit,form,uni-id-users').field('form_id.name,user_id.username,ispublic,info,create_date').where("`_id=="+param.form_id).get()
		return result
	}
};